<template>
  <!-- 登录接口调用
      1：封装api
      2:导入api方法
      3：在表单验证通过后调用
      4：调用成功
           token:存储
           跳转到首页
 -->
  <div class="login">
    <div class="box">
      <img src="@/assets/common/login-logo.png" alt="">
      <el-form ref="form" class="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <!-- <el-input v-model="form.password" prefix-icon="el-icon-lock" show-password @change="changeEvent" /> -->
          <!-- .native组件标签身上可以使用原生的事件 -->
          <el-input v-model="form.password" prefix-icon="el-icon-lock" show-password @keyup.enter.native="submit" />
        </el-form-item>
      </el-form>
      <el-button :loading="loading" type="primary" class="btn" @click="submit">{{ $t('login.login') }}</el-button>
    </div>

  </div>
</template>
<script>
import { sysLogin } from '@/api/user'
export default {
  data() {
    return {
      loading: false,
      form: {
        mobile: '13800000002', //	string	必须		手机号
        password: '123456' //	string	必须		密码
      },
      rules: {
        mobile: [{ required: true, message: '必填', trigger: 'change' },
          { pattern: /^1[0-9]\d{9}$/, message: '请正确输入手机号', trigger: 'change' }
        ], //	string	必须		手机号
        password: [{ required: true, message: '必填', trigger: 'change' }, {
          min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change'
        }] //	string	必须		密码
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          this.loading = true
          try {
            const res = await sysLogin(this.form)
            this.$message.success(this.$t('login.message'))
            this.$store.commit('user/SET_TOKEN', res.data)
            this.$router.push('/')
          } catch {
            this.loading = false
          }
        }
      })
    },
    changeEvent() {
    }
  }
}
</script>
<style lang="scss" scoped>
.login{
  height:100%;
  // vue-cli4.x  style内是~@代表src   /5.x   ~@代表src   @代表src
  background-image:url('~@/assets/common/login.jpg');
  background-repeat:no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box{
    // width: 450px;
    height:400px;
    // background-color: #fff;
    .form{
      margin-top:20px;
     ::v-deep .el-input__inner{
        height:47px;
        background-color: #d1e4ff;
      }
     ::v-deep   .el-form-item__error{
        color:#fff;
      }
    }
    .btn{
      width:100%;
      height:50px;
      font-size: 22px;
      background-color: #407ffe;
    }

  }
}
</style>
